<template>
  <el-menu
    :default-active="activeIndex"
    class="el-menu-demo"
    mode="horizontal"
    :ellipsis="false"
  >
    <el-menu-item index="0" @click="goLogin()"> LOGO </el-menu-item>
    <div class="flex-grow" />
    <el-sub-menu index="1" v-if="$route.query.isLogin === '1'">
      <template #title>个人中心</template>
      <el-menu-item index="1-1"> 我的订单 </el-menu-item>
      <el-menu-item index="1-2">item two</el-menu-item>
      <el-menu-item index="1-3">item three</el-menu-item>
      <el-menu-item index="1-4">注销</el-menu-item>
    </el-sub-menu>
    <el-menu-item
      index="3"
      v-if="$route.query.isLogin != '1'"
      @click="MenLogin()"
      >登录</el-menu-item
    >
  </el-menu>
</template>

<script lang="ts" setup>
import { inject, ref, onUnmounted, provide } from "vue";
import axios from "axios";
import { useRouter } from "vue-router";
const router = useRouter();
// const reload = inject("reload", Function, false);
const activeIndex = ref("3");
let token = ref(false);
function MenLogin() {
  router.push({ name: "login" });
}
function goLogin() {
  axios.get("http://localhost:9090/trainT/").then((response) => {
    if (response.status === 202) {
      (token.value = true),
        router.push({
          path: "/",
        });
      createWebSocket(response.data);
    }
  });
}
// const notifyMsg = inject("notifyMsg", Function, false);
const createWebSocket = inject("createWebSocket", Function, true);
// // webSocket origin
// var websocket = null;
// // 连接WebSocket
// const createWebSocket = (userId: string) => {
//   if ("WebSocket" in window) {
//     var protocol = window.location.protocol === "http:" ? "ws://" : "wss://";
//     websocket = new WebSocket(protocol + `localhost:8080/ws/${userId}`);
//   } else {
//     alert("该浏览器不支持WebSocket");
//   }
//   websocket.onopen = function (event: any) {
//     // heartCheck.reset().start();
//     console.log("建立 WebSocket 连接");
//   };

//   websocket.onclose = function (event: any) {
//     console.log("断开WebSocket连接");
//   };

//   websocket.onmessage = function (event: any) {
//     notifyMsg(event.data);
//   };

//   websocket.onerror = function (event: any) {
//     console.log("websocket通信发生错误");
//   };

//   provide("websocket", websocket);
//   /*   window.onbeforeunload = function (event: any) {
//     websocket.close();
//   }; */
// };
// provide("createWebSocket", createWebSocket);
// // 4.卸载前, 关闭链接
// onUnmounted(() => {
//   websocket.close();
// });
</script>

<style scoped>
.flex-grow {
  flex-grow: 1;
}
.item {
  margin-bottom: 5px;
  margin-right: 20px;
  margin-left: 20px;
  /* padding-bottom: 20px;
  height: 60%;
  width: 100%;
  background-color: #409eff; */
}
</style>
